/**
 * 
 */
package cn.kuaipai.utils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

/**
 * 字符处理的若干工具
 * @author zy
 */
public class StringTools {

	/**
	 * 字符进行URL编码
	 * @param source
	 * @param charset 为空默认为UTF-8
	 * @return
	 */
	public static String encode(String source ,String charset){
		if(StringUtils.isBlank(source))
			return "" ;
		if(StringUtils.isBlank(charset))
			charset = "UTF-8"  ;
		try {
			return java.net.URLEncoder.encode(source, charset).replace("+", "%20")
			.replace("*", "%2A").replace("%7E", "~")
			.replace("#", "%23");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
	public static String encode(String source){
		return encode(source,null);
	}
	/**
	 * 字符进行URL解码
	 * @param source 
	 * @param charset 为空默认为UTF-8
	 * @return
	 */
	public static String decode(String source ,String charset){
		if(StringUtils.isBlank(source))
			return "" ;
		if(StringUtils.isBlank(charset))
			charset = "UTF-8"  ;
		try {
			return java.net.URLDecoder.decode(source, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "" ;
	}
	public static String decode(String source){
		return decode(source,null);
	}
	/**
	 * 获取UUID，剔除中间的"-"，返回长度为32位
	 * @return
	 */
	public static String getUUID(){
		StringBuffer result = new StringBuffer();
		char ch [] = UUID.randomUUID().toString().toCharArray();
		for(char c : ch ){
			if(c == '-'){
				continue ;
			}
			result.append(c);
		}
		return result.toString();
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(getUUID() + " " + getUUID().length());
	}
}
